package com.example.yiparkapplication.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.example.yiparkapplication.R;
import com.example.yiparkapplication.constant.API;
import com.example.yiparkapplication.constant.Constant;
import com.example.yiparkapplication.util.BaseActivity;
import com.example.yiparkapplication.util.Common;
import com.rey.material.widget.Button;
import com.rey.material.widget.EditText;


import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ResetPasswordActivity extends BaseActivity {

    private Button registerButton;
    private Timer timer;
    private int mClick = 60;
    private String mSessionId;
    private String mCode = null;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password_layout);

        registerButton = (Button) findViewById(R.id.id_get_phone_code);
        registerButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                EditText mPhone = (EditText)findViewById(R.id.id_register_phone);
                String phone = mPhone.getText().toString().trim();
                if ("".equals(phone)){
                    Toast.makeText(ResetPasswordActivity.this, Constant.ON_PHONE, Toast.LENGTH_LONG).show();
                    return;
                }
                if (phone.length() != 11){
                    Toast.makeText(ResetPasswordActivity.this, Constant.ERROR_PHONE, Toast.LENGTH_LONG).show();
                    return;
                }
                if (timer != null){
                    myTimerTask myTimerTask = new myTimerTask();
                    timer.schedule(myTimerTask, 0, 1000);
                }else{
                    timer = new Timer();
                    mClick = 60;
                    myTimerTask mTimerTask = new myTimerTask();
                    timer.schedule(mTimerTask, 0 , 1000);
                }
                getPhoneCode(phone);
            }
        });

        Button reset = (Button) findViewById(R.id.id_reset_password);
        reset.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                EditText mPhone = (EditText)findViewById(R.id.id_register_phone);
                String phone = mPhone.getText().toString().trim();
                EditText mCheckPhoneCode = (EditText)findViewById(R.id.id_check_phone_code);
                mCode = mCheckPhoneCode.getText().toString().trim();
                EditText mPassword = (EditText)findViewById(R.id.id_register_password);
                String password = mPassword.toString().trim();
                resetPassword(phone, password);
            }
        });
    }

    private void resetPassword(String phone, String password){
        
    }

    private void getPhoneCode(String phone){
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(API.HOST + API.URL_GET_CODE + "?phoneNumber=" + phone + "&type=" + 1)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Common.failureMessage(e.getMessage(), getApplicationContext());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String tempResponse = response.body().string();
                JSONObject joOut = Common.tempResponseException(tempResponse, getApplicationContext());
                int code = joOut.getIntValue("code");
                if (code == 100){
                    JSONObject joIn = joOut.getJSONObject("object");
                    mSessionId = joIn.getString("sessionId");
                }
            }
        });
    }

    class myTimerTask extends TimerTask{
        Activity mActivity = new Activity();
        @Override
        public void run(){
            mActivity.runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    mClick--;
                    registerButton.setText(String.valueOf(mClick));
                    if (mClick == 0){
                        timer.cancel();
                        timer = null;
                        registerButton.setText(R.string.get_phone_code);
                    }
                }
            });
        }
    }

}
